//
//  TYBaseNavigationController.m
//  Class
//
//  Created by wbb on 2020/3/3.
//  Copyright © 2020 TY. All rights reserved.
//

#import "TYBaseNavigationController.h"

@interface TYBaseNavigationController () <UINavigationControllerDelegate>

@property (nonatomic, getter=isPushing) BOOL isPushing; ///< 是否正在push，防止多次push同一个VC
@property (nonatomic, copy) NSArray *hiddenNavListVCs;
@end

@implementation TYBaseNavigationController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationBar.translucent = NO;
    self.delegate = self;
    
    
    UINavigationBarAppearance *barAppearance = [[UINavigationBarAppearance alloc] init];
    barAppearance.backgroundColor = main_background_color;
    barAppearance.shadowColor = main_background_color; // 设置为红色，可替换为其他颜色
    barAppearance.shadowImage = [UIImage new];
    self.navigationBar.standardAppearance = barAppearance;
    self.navigationBar.scrollEdgeAppearance = barAppearance;
    self.navigationBar.tintColor = main_background_color;
    self.navigationBar.barTintColor = main_background_color;
}

- (void)pushViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    // 拦截多次push同一个VC
    if (self.isPushing) {
        return;
    }
    self.isPushing = YES;
    
    if (self.viewControllers.count > 0) {
        viewController.hidesBottomBarWhenPushed = YES;
        
        UIBarButtonItem *backItem = [[UIBarButtonItem alloc] initWithImage:[[UIImage imageNamed:@"back_image"] imageWithRenderingMode:UIImageRenderingModeAlwaysOriginal] style:UIBarButtonItemStylePlain target:self action:@selector(back)];
//        backItem.imageInsets = UIEdgeInsetsMake(0, -10, 0, 0);
        viewController.navigationItem.leftBarButtonItem = backItem;
        
        // 就有滑动返回功能
        self.interactivePopGestureRecognizer.delegate = nil;
    }
    [super pushViewController:viewController animated:animated];
}

- (void)navigationController:(UINavigationController *)navigationController didShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    
    self.isPushing = NO;
}
#pragma mark - UINavigationControllerDelegate

- (void)navigationController:(UINavigationController *)navigationController willShowViewController:(UIViewController *)viewController animated:(BOOL)animated {
    NSArray *list = @[@"YXJCVC",@"JKGJVC",@"GZGJVC"];
    // 判断如果是需要隐藏导航控制器的类，则隐藏
    if([WDGlobal shareInstance].isNeedSarch && [list containsObject:NSStringFromClass([viewController class])]) {
        // 这里设置导航栏隐藏
        [self setNavigationBarHidden:YES animated:animated];
        return;
    }
    
    for(int i=0;i<self.hiddenNavListVCs.count;i++){
        NSString *className = self.hiddenNavListVCs[i];
        if([viewController isKindOfClass:NSClassFromString(className)]){
            // 这里设置导航栏隐藏
            [self setNavigationBarHidden:YES animated:animated];
            return;
        }
    }
    
    [self setNavigationBarHidden:NO animated:animated];

    
}
- (void)back {
    [self popViewControllerAnimated:NO];
}
- (NSArray *)hiddenNavListVCs{
    if(!_hiddenNavListVCs){
        _hiddenNavListVCs = @[
            @"HomeViewController",
            @"MineViewController",
            @"LoginViewController",
            @"DangerResultVC"
        ];
    };
    return _hiddenNavListVCs;
}
@end

